package com.vhly.aapt;

import com.vhly.aapt.io.OrderInputStream;
import com.vhly.ddxdecompiler.dexreader.DexReader;
import com.vhly.ddxdecompiler.dexreader.file.DexFile;
import com.vhly.ddxdecompiler.dexreader.util.FindUtil;

import java.io.*;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-4-26
 * Time: 16:14:49
 */
public class Main {
    public static void main(String[] args) {
        int argc = args.length;
        if (argc != 2) {
            usage();
        } else {
            String apkfile = args[0];
            String targetDir = args[1];
            File f = new File(apkfile);
            if (!f.exists()) {
                System.out.print("Error Input APK file is not Exists!");
            } else {
                File td = new File(targetDir);
                if (!td.exists()) {
                    td.mkdirs();
                }
                JarFile jar = null;
                InputStream in = null;
                OrderInputStream din = null;
                ResXML xml;
                try {
                    jar = new JarFile(f);
//                    JarEntry en = jar.getJarEntry("AndroidManifest.xml");
//                    if(en != null){
//                        in = jar.getInputStream(en);
//                        din = new OrderInputStream(in,1);
//                        xml = new ResXML();
//                        xml.parse(din);
//                        String s = xml.toXML();
//                        writeFileXML(td,"AndroidManifest.xml",s);
//                        xml = null;
//                    }
                    JarEntry dexEntry = jar.getJarEntry("classes.dex");
                    DexFile dexFile = null;
                    if(dexEntry != null){
                        InputStream ins = jar.getInputStream(dexEntry);
                        dexFile = DexReader.parse(ins);
                        System.out.println("dexFile = " + dexFile);
                        FindUtil.findClassDef(dexFile, "com.android.vending.ChainAction");
                    }
                    Enumeration<JarEntry> entries = jar.entries();
                    JarEntry je;
                    String en;
                    while (entries.hasMoreElements()) {
                        je = entries.nextElement();
                        en = je.getName();
                        if (en.toLowerCase().endsWith(".xml")) {
                            in = jar.getInputStream(je);
                            din = new OrderInputStream(in, 1);
                            xml = new ResXML();
                            xml.parse(din);
                            String s = xml.toXML();
                            writeFileXML(td, en, s);
                            xml = null;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (din != null) {
                        try {
                            din.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }
    }

    private static void writeFileXML(File td, String filePath, String content) {
        if (td != null && filePath != null && content != null) {
            File ff = new File(td, filePath);
            String pf = ff.getParent();
            ff = new File(pf);
            if (!ff.exists()) {
                ff.mkdirs();
            }
            ff = new File(td, filePath);
            FileWriter fw = null;
            try {
                fw = new FileWriter(ff);
                fw.write(content);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fw != null) {
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private static void usage() {
        System.out.println("Usage: com.vhly.aapt.Main <apkfile> <targetdir>");
    }
}
